<?php
/**
 * 数据格式化
 *
 */

namespace dragon\Traits;

use think\Config;
use think\Db;

trait Format
{
    protected $formatPattern = array(
        'timestamp' => 'time',
        'last_time' => 'time',
        'amount' => 'rmb',
   //     'type' => 'Company.type,config', // 有config就找config里的
   //     "factory_id" => 'Company.title', //  '想要改的字段' => '类名.字段'
    );

    public function format($data, $pattern = array())
    {
        $paginate = [];
        if (!$data) return $data;
        $pattern = array_merge($this->formatPattern, $pattern);
        if (is_one_dimension($data)) $data = array('_key_' => $data);
        if (isset($data['current_page']) && isset($data['data']) && is_array($data['data'])) {
            $paginate = $data;
            $data = $data['data'];
        }
        $tmp = array();
        foreach ($data as $key => $val) {
            foreach ($val as $k => $v) {
                if (isset($pattern[$k]) and $format = $pattern[$k])
                    switch ($format) {
                        case 'time':
                            $data[$key][$k . '_format'] = $v ? date('Y-m-d H:i:s', $v) : '';
                            break;
                        case 'rmb':
                            $data[$key][$k . '_format'] = '￥' . number_format($v, 2);
                            break;
                        case 'num':
                        case 'num.1':
                        case 'num.2':
                        case 'num.3':
                            $data[$key][$k . '_format'] = number_format($v, (int)substr($format, -1));
                            break;
                        default:
                            // V 为 数组
                            if (preg_match('/\.s$/', $format)) {
                                $format = preg_replace('/\.s$/', '', $format);
                                is_string($v) and $v = explode(',', $v);
                            }

                            isset($tmp[$format]) || $tmp[$format] = array();
                            if (is_array($v)) {
                                foreach ($v as $_v) {
                                    isset($tmp[$format][$_v]) || $tmp[$format][$_v] = $_v;
                                    $data[$key][$k . '_format'][$_v] = &$tmp[$format][$_v];
                                }
                            } else {
                                isset($tmp[$format][$v]) || $tmp[$format][$v] = $v;
                                $data[$key][$k . '_format'] = &$tmp[$format][$v];
                            }
                    }
            }
        }

        // 这里暂时只根据ID识别
        foreach ($tmp as $key => $val) {

            $_key = explode(',', $key);
            $target = explode('.', $_key[0]);

            isset($_key[1]) || $_key[1] = '';
            switch (true) {
                case !!strstr($_key[1], 'config'):
                    $class = Config::getClassByAlias($target[0]);
                    $config = $class::config($target[1]);
                    foreach ($val as $k => $v) {
                        $tmp[$key][$v] = isset($target[2]) ? $config[$v][$target[2]] : $config[$v];
                    }
                    break;
                default:

                    $list = Db::name("$target[0]")->where('id', 'in', $val)->select();

                    foreach ($list as $k => $v) {
                        $tmp[$key][$v['id']] = isset($v[$target[1] . '_format']) ?: $v[$target[1]];
                    }
            }
        }
        if ($paginate) {
            $paginate['data'] = $data;
            return $paginate;
        } else {
            return isset($data['_key_']) ? $data['_key_'] : $data;
        }

    }
}
